﻿/*********************弘石项目三组************************
*  功能描述：项目前期信息
*  作者：刘伟康
*  时间：2021-12-13
*  修改人：刘伟康
*  修改时间：2021-12-13
*********************弘石项目三组************************/
using Hong.Business.Business_CustomerManagement;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace WeHongshibApI.Controllers.Controllers_CustomerManagement
{
    [ApiExplorerSettings(GroupName = "Customer")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CustinformationController : ControllerBase
    {
        #region 构造器注入
        private readonly CustinformationBusiness _custInfo;
        public CustinformationController(CustinformationBusiness custInfo)
        {
            _custInfo = custInfo;
        }
        #endregion

        #region 项目前期信息
        #region 项目前期信息的显示查询
        /// <summary>
        /// 文档信息显示查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAccessory(string acsoryIdNmae, DateTime? acsoryIdTime, int pageIndex = 1, int pageSize = 10)
        {
            int totalCount;
            var list = _custInfo.GetAccessory(acsoryIdNmae, acsoryIdTime, pageIndex, pageSize, out totalCount);
            return Ok(new { list, totalCount });
        }
        #endregion

        #region 项目前期信息的批量操作
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DeleteAccessory(string ids)
        {
            return Ok(_custInfo.DeleteAccessory(ids));
        }
        #endregion

        #region 导出excel
        [HttpGet]
        public IActionResult DownloadExcel()
        {
            int totalCount;
            var list = _custInfo.GetAccessory(null, null, 1, 100000, out totalCount);
            var values = new List<Dictionary<string, string>>();
            foreach (var item in list)
            {
                values.Add(new Dictionary<string, string> {
                    {"序号",$"{item.AcsoryId}" },{"文件名",$"{item.AcsoryIdNmae}" },{"上传时间",$"{item.AcsoryIdTime}" },
                    {"文件大小",$"{item.AcsoryIdSize}" },{"文件类型",$"{item.AcsoryIdType}" },{"用户",$"{item.AcsoryIdUser}" },
                    {"外键",$"{item.CustomerId}" }
                });
            }
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(values);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "applicatio/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "项目前期信息.xlsx"
            };
        }
        #endregion
        #endregion

    }
}
